# -*- coding: utf-8 -*-

import socket
import threading
import time


def Send(s):
    try:
        while True:
            data = input().encode('utf-8')
            s.send(data)
            if data.decode('utf-8') == 'quit':
                break
    except:
        sock.close()


def Recv(s):
    try:
        while True:
            data = s.recv(1024).decode('utf-8')
            if not data:
                break
            print(data)
    except:
        sock.close()

ip = socket.gethostbyname(socket.gethostname())
print('input your IP:%s' % ip)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, 8888))
try:
    name = input("input your username: ")
    sock.send(name.encode('utf-8'))
    send = threading.Thread(target=Send, args=(sock,))
    send.start()
    recv = threading.Thread(target=Recv, args=(sock,))
    recv.start()
except:
    sock.close()
